Imports VB = Microsoft.VisualBasic

Public Class formNESColor
    WithEvents pic As PictureBox
    '*********************************************************
    'Global variables used to send data to the calling routine
    '*********************************************************
    Public bytNESColor As Byte = &HFF   'Sends back the selected color

    '*****************************************************************
    'Global variables used to receive data from to the calling routine
    '*****************************************************************
    Public FormLeft As Integer  'User to position this form on the screen

    '******************
    'Internal variables
    '******************
    Dim bytColor As Byte = &HFF     'Internal tracking of the selected color.  &HFF is invalid and indicates the user has not selected a color

    Private Sub formNESColor_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Center the form on the screen
        Me.Top = (Screen.PrimaryScreen.Bounds.Height - Me.Height) / 2
        Me.Left = FormLeft - Me.Width / 2

        'Initialize size parameters - necessary to fit other OSs and resolutions
        Me.Width = 1386
        Me.Height = 661

        tlpMain.Width = 1248
        tlpMain.Height = 343

        grpSelection.Width = 126
        grpSelection.Height = 141

        grpSelection.Left = 315
        grpSelection.Top = 434

        btnOK.Left = 794
        btnOK.Top = 461

        For i As Integer = 0 To &H3F
            'Dynamically create all the pictureboxes at run-time.  Much easier to maintain than creating them all at design-time
            pic = New PictureBox
            pic.Name = "pic" & Hex(i).PadLeft(2, "0")   'i.e. pic00 through pic3F
            pic.Margin = New Padding(8, 8, 8, 8)
            pic.Dock = DockStyle.Fill
            pic.BackColor = NEStoRGB(i)
            AddHandler pic.Click, AddressOf pic_Click   'Point all the eventhandlers to the same routine for ease of handling

            tlpMain.Controls.Add(pic)   'Controls are added in the correct order, top left to bottom right, rows first
        Next i
    End Sub

    'This handles ALL the click events.  For all 64 color picture boxes
    Private Sub pic_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        ColorSelect("&H" & VB.Right(sender.name, 2))    'The right 2 characters of the picturebox name will be 00-3F - corresponding to the correct NES color
    End Sub

    Private Sub ColorSelect(ByVal pbytNESColor As Byte)
        picSelection.BackColor = NEStoRGB(pbytNESColor) 'Display the currently selected color
        bytColor = pbytNESColor 'Set the internal color value; will be transferred back to the calling form
    End Sub

    Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
        'Only send the color back if the OK button was pressed
        'This is done because if the user closes the form (e.g. via the 'X' in the top right corner), then the color will not be altered in the main form
        bytNESColor = bytColor
        Me.Close()
    End Sub
End Class